package net 
{
	import flash.utils.Dictionary;
	import log.Log;
	/**
	 * ...
	 * @author carlor
	 */
	public class GateWay 
	{
		private var callbacks: Dictionary = new Dictionary();
		
		public function GateWay() 
		{
			
		}
		
		/**
		 * 注册功能模块的回调函数。 
		 * 
		 */	
		public function register(method: int, callback: Function): void
		{
			if (! callbacks.hasOwnProperty(method)) {
				callbacks[method] = callback;
			}
		}
		/**
		 * 分发消息封包到指定功能模块。 
		 * 
		 */		
		public function dispatch(header: int, message: Object): void
		{
			try {
				if (callbacks.hasOwnProperty(header))
				{
					//Log.formatErrorLog("recv message id=%% message=%%", [header, message]);
					Engine.dialogManager.handlePacket("onShowMessage", "recv message id=" + header + " message=" +message.toString());
					callbacks[header](message);	
				}
				else
				{
					//Log.formatErrorLog("recv message id=%% message=%%, no callbackFunc", [header, message]);
					Engine.dialogManager.handlePacket("onShowMessage", "recv message error id=" + header + " message=" +message.toString());
					//throw new Error("[GateWay] not find callback mapping with the header.");
				}
			}catch (err: Error) {
				Log.errorLog(err.getStackTrace());
			}
			
		}
		
	}

}